import api from '../api';

const getListAsync = async (data) => {
  return await api.postWithToken({
    command: 'I_Power_GetPowers',
    data: {
      page_size: 999,
      page_index: 1
    }
  });
};

function setData(powers, parent_id) {
  let trees = [];
  for (let i = 0; i < powers.length; i++) {
    const p = powers[i];

    if (p.parent_id == parent_id) {
      let tree = {
        id: p.id,
        name: p.name,
        is_menu: p.is_menu == "True",
        parent_id: p.parent_id,
      };

      let children = this.setData(powers, p.parent_id);
      if (children.length > 0) tree.children = children;

      trees.push(tree);
    }
  }

  return trees;
}

export default {
  getListAsync,
  setData,
}